import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution5 {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param pRoot TreeNode类 
     * @return bool布尔型
     */
    public boolean IsBalanced_Solution (TreeNode pRoot) {
        // write code here
        if(pRoot == null){
            return true;
        }
        int left = height(pRoot.left);
        int right = height(pRoot.right);
        if(Math.abs(left-right) > 1){
            return false;
        }
        return IsBalanced_Solution(pRoot.left) && IsBalanced_Solution(pRoot.right);
    }
    public int height(TreeNode pRoot){
        if(pRoot == null){
            return 0;
        }
        return 1 + Math.max(height(pRoot.left), height(pRoot.right));
    }
}